package com.sanjiahantang.ythshopping.adapter.home;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanjiahantang.shopping.uilibrary.uiutils.ScreenUtils;
import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.activity.MainActivity;
import com.sanjiahantang.ythshopping.activity.me.JsWebActivity;
import com.sanjiahantang.ythshopping.config.ConfigValue;
import com.sanjiahantang.ythshopping.model.NearbyShop;
import com.sanjiahantang.ythshopping.utils.Utils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017/7/30.
 */

public class AllianceHolder extends MyBaseViewHolder {
    public static final int TYPE_HOME = 0x123;
    public static final int TYPE_NEAR_BY = 0x456;
    private List<View> gridList;
    private List<ImageView> imageviews;
    private Context context;
    private ViewPager vpMenu;
    private LinearLayout llImage;
    private AlPageAdapter pageAdapter;
    private int type;
    private int count = 0;
    private int height = 150;
    private int textHeight = 50;


    private List<NearbyShop.ShopList> datas;




    public AllianceHolder(final Context context, View itemView, final List<NearbyShop.ShopList> datas,int type) {
        super(itemView);
        this.context = context;
        this.datas = datas;
        this.type = type;

        this.context = context;
        this.datas = datas;
        vpMenu = (ViewPager) itemView.findViewById(R.id.vp_home_menu);
        llImage= (LinearLayout) itemView.findViewById(R.id.ll_home_page);
        gridList=new ArrayList<>();
        View title = itemView.findViewById(R.id.title);
        TextView tv_type = (TextView) itemView.findViewById(R.id.tv_type);
        tv_type.setText("联盟商家");

        if (type == TYPE_HOME){
            count = 4;
        }else {
            count = 2;
        }
        height = (Utils.px2dip(context,ScreenUtils.getScreenWidth(context)) - 5 * (count + 1)) / count + textHeight;
        vpMenu.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,Utils.dip2px(context,height)));
        if (datas.size() < 1){
            vpMenu.setVisibility(View.GONE);
            title.setVisibility(View.GONE);
            llImage.setVisibility(View.GONE);
        }else {
            vpMenu.setVisibility(View.VISIBLE);
            title.setVisibility(View.VISIBLE);
        }
    }

    @Override
    void bindData() {
        imageviews=new ArrayList<>();

        while (datas.size()!=0)
        {
            List<NearbyShop.ShopList> itemgrid=new ArrayList<>();

            for (int i=0;i < count;i++)
            {
                if (datas.size()!=0)
                {
                    itemgrid.add(datas.get(0));
                    datas.remove(0);
                }else {
                    break;
                }
            }
            GridView gridView=new MyGridView(context);
            gridView.setNumColumns(count);
            gridView.setHorizontalSpacing(Utils.dip2px(context,5));
            gridView.setAdapter(new SHopAdapter(itemgrid));

            gridList.add(gridView);
            ImageView imageview=new ImageView(context);
            LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
                    10, 10);
            layoutParam.setMargins(10, 0, 10, 0);
            imageview.setLayoutParams(layoutParam);
            if (imageviews.size()==0)
            {
                imageview.setImageDrawable(context.getResources().getDrawable(R.drawable.shape_dot_red));

            }else{
                imageview.setImageDrawable(context.getResources().getDrawable(R.drawable.shape_dot_normal));
            }
            imageviews.add(imageview);
            llImage.addView(imageview);


        }
        pageAdapter=new AlPageAdapter(gridList);
        vpMenu.setAdapter(pageAdapter);
        vpMenu.setOnPageChangeListener(new AlPageChangeListener());

        vpMenu.setPadding(Utils.dip2px(context,5),0,Utils.dip2px(context,5),0);

    }



    public class SHopAdapter extends BaseAdapter {

        List<NearbyShop.ShopList> data;
        LayoutInflater inflater;
        Picasso picasso;

        public SHopAdapter(List<NearbyShop.ShopList> data){
            this.data=data;
            inflater=LayoutInflater.from(context);
            picasso = Picasso.with(context);
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int i) {
            return data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHodle viewhodle;
            if (view==null)
            {
                view=inflater.inflate(R.layout.item_home_shop,null);
                viewhodle=new ViewHodle();
                viewhodle.iv_pic = (ImageView) view.findViewById(R.id.nearby_shop_img);
                viewhodle.tv_name= (TextView) view.findViewById(R.id.nearby_shop_name);
                viewhodle.iv_pic.setLayoutParams(new LinearLayout.LayoutParams(Utils.dip2px(context,height - textHeight),Utils.dip2px(context,height - textHeight)));
                view.setTag(viewhodle);
            }else {
                viewhodle= (ViewHodle) view.getTag();
            }
            if (data.size() > 0){

                viewhodle.tv_name.setText(data.get(i).getShop_name());

                picasso.load(ConfigValue.IMG_IP + data.get(i).getShop_img())
                        .resize(Utils.dip2px(context,ConfigValue.IMAGE_SCALE),Utils.dip2px(context,ConfigValue.IMAGE_SCALE))
                        .centerCrop()
                        .into(viewhodle.iv_pic);
                viewhodle.iv_pic.setTag(R.id.decode,ConfigValue.IMG_IP + data.get(i).getShop_img());
                final String url = data.get(i).getShop_url();
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, JsWebActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("url", url);
                        intent.putExtras(bundle);
                        ((Activity)context).startActivityForResult(intent, MainActivity.RESULT_NEAR);
                    }
                });
            }


            return view;
        }

        public class ViewHodle
        {
            TextView tv_name;
            ImageView iv_pic;
        }
    }


    private final class AlPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int arg0) {
            for (int i = 0; i < imageviews.size(); i++) {
                if (arg0 != i) {
                    imageviews.get(i).setImageDrawable(context.getResources().getDrawable(R.drawable.shape_dot_normal));
                } else {
                    imageviews.get(i).setImageDrawable(context.getResources().getDrawable(R.drawable.shape_dot_red));
                }
            }
        }
    }



    private class AlPageAdapter extends PagerAdapter {
        private List<View> views = null;

        public AlPageAdapter(List<View> views) {
            this.views = views;
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView(views.get(position));

            /*
            GridView gv = (GridView) views.get(position);
            for (int i = 0,max = gv.getChildCount();i < max;i++){
                ImageView iv = (ImageView) gv.getChildAt(i).findViewById(R.id.nearby_shop_img);
                Utils.releaseImageViewResouce(iv);
                iv.setImageBitmap(null);
            }
            */
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public Object instantiateItem(View container, final int position) {
            ((ViewPager) container).addView(views.get(position), 0);
            return views.get(position);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }



}
